import {createRouter, createWebHistory} from "vue-router";
import Home from "../views/Home.vue";
import Request from "../utils/request"
import AxiosOrigin from "axios";
import Methods from "../utils/Methods";
const routes = [
    {
        path: '/',
        redirect: '/dashboard'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '数据中心'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/home/Dashboard.vue")
            },
            {
                path: "/area_data",
                name: "area_data",
                meta: {
                    title: '数据管理'
                },
                component: () => import ( /* webpackChunkName: "Slide" */ "../views/area/area_data.vue")
            },
            {
                path: "/project",
                name: "project",
                meta: {
                    title: '项目管理'
                },
                component: () => import ( /* webpackChunkName: "Slide" */ "../views/area/project.vue")
            },
            {
                path: "/adminlist",
                name: "adminlist",
                meta: {
                    title: '管理员列表'
                },
                component: () => import ( /* webpackChunkName: "tabs" */ "../views/admin/AdminList.vue")
            },{
                path: "/rolelist",
                name: "rolelist",
                meta: {
                    title: '角色列表'
                },
                component: () => import ( /* webpackChunkName: "tabs" */ "../views/admin/RoleList.vue")
            },{
                path: "/nodelist",
                name: "nodelist",
                meta: {
                    title: '接口权限'
                },
                component: () => import ( /* webpackChunkName: "tabs" */ "../views/admin/NodeList.vue")
            }, {
                path: "/routelist",
                name: "routelist",
                meta: {
                    title: '路由权限'
                },
                component: () => import ( /* webpackChunkName: "tabs" */ "../views/admin/RouteList.vue")
            },
            {
                path: "/loginrecord",
                name: "loginrecord",
                meta: {
                    title: '登录日志'
                },
                component: () => import ( /* webpackChunkName: "tabs" */ "../views/syslog/LoginRecord.vue")
            },{
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../views/403.vue')
            }, {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/User.vue')
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Login.vue")
    },
    {
        path: "/icon",
        name: "icon",
        meta: {
            title: '图标'
        },
        component: () => import ( /* webpackChunkName: "tabs" */ "../views/Icon.vue")
    },
    {
        path: '/404',
        name: '404',
        meta: {
            title: '找不到页面'
        },
        component: () => import (/* webpackChunkName: "404" */ '../views/404.vue')
    },
    {
        path: '/test',
        name: '测试页',
        meta: {
            title: '测试页'
        },
        component: () => import (/* webpackChunkName: "404" */ '../views/test.vue')
    }
];

const router = createRouter({
    history: createWebHistory('/zhwl_admin'),
    routes
})

const allow = ['/login','/','/icon','/test']

router.beforeEach((to, from, next) => {
    document.title = to.meta.title;
    let token = Methods.getLocalStorage('token');
    if(to.matched.length === 0) {
        next({path:'/404'})
    }else {
        if(allow.indexOf(to.path) === -1) {
            if(token) {
                let last_refresh_time = Methods.getLocalStorage('last_refresh_time')
                if(last_refresh_time && (new Date().getTime()/1000 - parseFloat(last_refresh_time)) > 3600) {
                    let data = { token:token }
                    Request.post('/manage/index/refreshToken',data).then(res => {
                        // console.log(res.data,'-------refreshToken')
                        console.log('refreshToken成功')
                        Methods.setLocalStorage('token',res.token)
                        Methods.setLocalStorage('last_refresh_time',new Date().getTime()/1000)
                        next()
                    }).catch(err => {
                        console.log(err,'-----refreshToken error')
                    })
                }else {
                    // console.log('时间不够无需刷新' + last_refresh_time)
                    next()
                }
            }else {
                next('/login')
            }
        }else {
            if(to.path === '/login') {
                // console.log('正在访问login')
                if(token) {
                    let data = {token: token}
                    AxiosOrigin.post('/manage/login/checkToken', data).then(res => {
                        if(res.data.code === -3 || res.data.code === -6) {
                            next()
                        }else if (res.data.code === 1) {
                            //如果token还能用, 则返回首页
                            next('/')
                        }else {
                            alert('router error: ' + JSON.stringify(res.data))
                        }
                    }).catch(err => {
                        console.log(err,'-------router error login')
                    })
                }else {
                    next()
                }
            }else {
                next()
            }
        }
    }

});

export default router;
